package com.callbox.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;

import com.callbox.entity.*;

public class ClsMessageDAO {
	
	private final String MESSAGE_TABLE = "Employees";
	
	private static ClsMessageDAO instance = null;	
	public static ClsMessageDAO Instance() {		
		if(instance == null){
			instance = new ClsMessageDAO();
		}		
		return instance;
	}
	
	public List<ClsMessages> ListByMobile(Context context, int IdMobile ) {
		
		ArrayList<ClsMessages> ListMessages = new ArrayList<ClsMessages>();
		
		SqliteHelper Connection = ClsConnection.GetSqliteAcces(context);
		SQLiteDatabase db = Connection.GetDatabase();
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

		String [] sqlSelect = {"0 _id","Number","ReceiptDate","Duration","Time","NameCaller","PathRecord"};		
		String sqlTables = "Employees";
		

		qb.setTables(sqlTables);
		Cursor result = qb.query(db, sqlSelect, null, null,
				null, null, null);
		
		if (result.moveToFirst())
			do {
				
				
				ClsMessages objMessage = new ClsMessages();								
				objMessage.setNumberFrom(result.getString(result.getColumnIndex(sqlSelect[1])));				
				objMessage.setReceiptDate(result.getString(result.getColumnIndex(sqlSelect[2])));
				objMessage.setDuration(result.getString(result.getColumnIndex(sqlSelect[3])));
				objMessage.setTime(result.getString(result.getColumnIndex(sqlSelect[4])));
				objMessage.setName(result.getString(result.getColumnIndex(sqlSelect[5])));
				objMessage.setPathResource(result.getString(result.getColumnIndex(sqlSelect[6])));
				
				ListMessages.add(objMessage);
				
			} while(result.moveToNext());
		
		return ListMessages;
	}

	public Boolean Insert( Context context, ClsMessages m ) {
		
		
		SqliteHelper Connection = ClsConnection.GetSqliteAcces(context);
		SQLiteDatabase db = Connection.GetDatabase();		
		ContentValues newValues = new ContentValues();
		
		newValues.put("Number", m.getNumberFrom());
		newValues.put("ReceiptDate", m.getReceiptDate());
		newValues.put("Duration", m.getDuration());		
		newValues.put("Time", m.getTime());
		newValues.put("NameCaller", m.getName());
		newValues.put("PathRecord", m.getPathResource());
		
		db.insert(MESSAGE_TABLE, null, newValues);
		
		return true;
		
	}

}

